package com.cnunicom.modules.business.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cnunicom.modules.business.entity.BusinessDeptEntity;
import com.cnunicom.modules.business.service.BusinessDeptService;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.R;



/**
 * 部门表
 *
 * @author dingziyuan
 * @email dzy998877@163.com
 * @date 2019-07-19 10:08:50
 */
@RestController
@RequestMapping("business/businessdept")
public class BusinessDeptController {
    @Autowired
    private BusinessDeptService businessDeptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:businessdept:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = businessDeptService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:businessdept:info")
    public R info(@PathVariable("id") Long id){
		BusinessDeptEntity businessDept = businessDeptService.getById(id);

        return R.ok().put("businessDept", businessDept);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:businessdept:save")
    public R save(@RequestBody BusinessDeptEntity businessDept){
        String[] split = businessDept.getName().split(",");
        for (String s : split) {
            businessDept.setName(s);
            businessDept.setCreatedTime(new Date());
            businessDeptService.save(businessDept);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:businessdept:update")
    public R update(@RequestBody BusinessDeptEntity businessDept){
		businessDeptService.updateById(businessDept);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:businessdept:delete")
    public R delete(@RequestBody Long[] ids){
		businessDeptService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
